home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 19 / Amiga Plus Leser CD 19.iso / Tools / Freeware / ttengine-5.0 / Examples / Background / background.c next >
Encoding:
C/C++ Source or Header  |  2002-11-05  |  8.7 KB  |  243 lines

  1. /* test ttrender */
  2.  
  3. #define __NOLIBBASE__
  4.  
  5. #include <proto/dos.h>
  6. #include <proto/exec.h>
  7. #include <proto/intuition.h>
  8. #include <proto/graphics.h>
  9. #include <proto/ttengine.h>
  10. #include <proto/asl.h>
  11. #include <proto/datatypes.h>
  12. #include <datatypes/pictureclass.h>
  13.  
  14. #include <libraries/ttengine.h>
  15.  
  16. extern struct Library *SysBase, *DOSBase;
  17.  
  18. struct Library *TTEngineBase, *IntuitionBase, *GfxBase, *AslBase, *DataTypesBase;
  19. struct BitMap *PicBitMap;
  20. Object *Picture;
  21.  
  22. /*----------------------------------------------------------------------------------------------------*/
  23.  
  24. static STRPTR get_font_name(struct Library *AslBase)
  25.   {
  26.     struct FileRequester *freq;
  27.     STRPTR name = NULL;
  28.  
  29.     if (freq = AllocAslRequestTags(ASL_FileRequest, TAG_END))
  30.       {
  31.         if (AslRequestTags(freq,
  32.           ASLFR_TitleText, (ULONG)"Select TrueType font",
  33.           ASLFR_InitialDrawer, (ULONG)"FONTS:",
  34.           ASLFR_DoPatterns, TRUE,
  35.           ASLFR_InitialPattern, (ULONG)"#?.ttf",
  36.           ASLFR_RejectIcons, TRUE,
  37.           TAG_END))
  38.           {
  39.             ULONG namelen = strlen(freq->fr_File) + strlen(freq->fr_Drawer) + 4;
  40.  
  41.             if (name = AllocVec(namelen + 1, MEMF_ANY | MEMF_CLEAR))
  42.               {
  43.                 strncpy(name, freq->fr_Drawer, namelen);
  44.                 AddPart(name, freq->fr_File, namelen);
  45.               }
  46.           }
  47.         FreeAslRequest(freq);
  48.       }
  49.     return name;
  50.   }
  51.  
  52. /*----------------------------------------------------------------------------------------------------*/
  53.  
  54. static VOID free_font_name(STRPTR name)
  55.   {
  56.     if (name) FreeVec(name);
  57.   }
  58.  
  59. /*----------------------------------------------------------------------------------------------------*/
  60.  
  61. VOID ctext (struct Window *w, STRPTR text, ULONG y)
  62.   {
  63.     UWORD tl, bl;
  64.     struct RastPort *rp = w->RPort;
  65.  
  66.     tl = strlen(text);
  67.     bl = TT_TextLength(rp, text, tl);
  68.     Move(rp, ((448 - bl) >> 1) + w->BorderLeft, y);
  69.     TT_Text(rp, text, tl);
  70.   }
  71.  
  72. /*----------------------------------------------------------------------------------------------------*/
  73.  
  74. BOOL init(VOID)
  75.   {
  76.     if (!(GfxBase = OpenLibrary("graphics.library", 39))) return FALSE;
  77.     if (!(IntuitionBase = OpenLibrary("intuition.library", 39))) return FALSE;
  78.     if (!(AslBase = OpenLibrary("asl.library", 38))) return FALSE;
  79.     if (!(TTEngineBase = OpenLibrary("ttengine.library", 4))) return FALSE;
  80.     if (!(DataTypesBase = OpenLibrary("datatypes.library", 39))) return FALSE;
  81.     return TRUE;
  82.   }
  83.  
  84. /*----------------------------------------------------------------------------------------------------*/
  85.  
  86. VOID cleanup(VOID)
  87.   {
  88.     if (DataTypesBase) CloseLibrary(DataTypesBase);
  89.     if (TTEngineBase) CloseLibrary(TTEngineBase);
  90.     if (AslBase) CloseLibrary(AslBase);
  91.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  92.     if (GfxBase) CloseLibrary(GfxBase);
  93.     return;
  94.   }
  95.  
  96. /*----------------------------------------------------------------------------------------------------*/
  97.  
  98. VOID load_picture(struct Window *w)
  99.   {
  100.     if (Picture = NewDTObject("PROGDIR:picture",
  101.       DTA_GroupID,    GID_PICTURE,
  102.       PDTA_Remap,     TRUE,
  103.       PDTA_DestMode,  PMODE_V43,
  104.       PDTA_Screen,    (ULONG)w->WScreen,
  105.       TAG_END))
  106.       {
  107.         DoDTMethod(Picture, NULL, NULL, DTM_PROCLAYOUT, NULL, DTSIF_NEWSIZE);
  108.         GetDTAttrs(Picture, PDTA_DestBitMap, (ULONG)&PicBitMap, TAG_END);
  109.       }
  110.     return;
  111.   }
  112.  
  113. /*----------------------------------------------------------------------------------------------------*/
  114.  
  115. int Main (void)
  116.   {
  117.     struct Window *win;
  118.     STRPTR fontname;
  119.     APTR font;
  120.  
  121.     if (init())
  122.       {
  123.         if (fontname = get_font_name(AslBase))
  124.           {
  125.             if (win = OpenWindowTags(NULL,
  126.               WA_Top, 25,
  127.               WA_Left, 0,
  128.               WA_InnerWidth, 448,
  129.               WA_InnerHeight, 262,
  130.               WA_CloseGadget, TRUE,
  131.               WA_DragBar, TRUE,
  132.               WA_DepthGadget, TRUE,
  133.               WA_Activate, TRUE,
  134.               WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_INTUITICKS,
  135.               WA_Title, (ULONG)"TTEngine font picture demo",
  136.               WA_ScreenTitle, (ULONG)"TTEngine demo",
  137.               TAG_END))
  138.               {
  139.                 ULONG sigmask, signals, pen;
  140.                 BOOL running = TRUE;
  141.                 struct RastPort *rp = win->RPort;
  142.                 WORD cnt = 0;
  143.                 ULONG r = 0, g = 0x7FFFFFFF, b = 0xFFFFFFFF, icl;
  144.  
  145.                 load_picture(win);
  146.  
  147.                 if (font = TT_OpenFont(
  148.                   TT_FontFile, (ULONG)fontname,
  149.                   TT_FontSize, 26,
  150.                 TAG_END))
  151.                   {
  152.                     SetDrMd(rp, JAM1);
  153.                     if (TT_SetFont(rp, font))
  154.                       {
  155.                         sigmask = SIGBREAKF_CTRL_C | (1 << win->UserPort->mp_SigBit);
  156.                         while (running)
  157.                           {
  158.                             signals = Wait(sigmask);
  159.                             if (signals & SIGBREAKF_CTRL_C) running = FALSE;
  160.                             if (signals & (1 << win->UserPort->mp_SigBit))
  161.                               {
  162.                                 struct IntuiMessage *imsg;
  163.  
  164.                                 while (imsg = (struct IntuiMessage*)GetMsg(win->UserPort))
  165.                                   {
  166.                                     icl = imsg->Class;
  167.                                     ReplyMsg((struct Message*)imsg);
  168.                                     if (icl == IDCMP_CLOSEWINDOW) running = FALSE;
  169.                                     if (icl == IDCMP_INTUITICKS)
  170.                                       {
  171.                                         if (++cnt > 9)
  172.                                           {
  173.                                             ULONG pen;
  174.  
  175.                                             if (PicBitMap)
  176.                                               BltBitMapRastPort(PicBitMap, 0, 0, rp, win->BorderLeft, win->BorderTop, 448, 262, 0xC0);
  177.  
  178.                                             cnt = 0;
  179.                                             r += 0x0817736F;
  180.                                             g += 0x0A27E836;
  181.                                             b += 0x0C3A47D4;
  182.  
  183.                                             pen = ObtainBestPen(win->WScreen->ViewPort.ColorMap, r, g, b, TAG_END);
  184.                                             SetAPen(rp, pen);
  185.  
  186.                                             TT_SetAttrs(rp,
  187.                                               TT_Window, (ULONG)win,
  188.                                               TT_Antialias, TT_Antialias_On,
  189.                                               TT_Transparency, 0,
  190.                                             TAG_END);
  191.  
  192.                                             ctext(win, "You can see here how the text is", 40);
  193.                                             ctext(win, "smoothed on background picture.", 66);
  194.  
  195.                                             TT_SetAttrs(rp,
  196.                                               TT_Antialias, TT_Antialias_Off,
  197.                                             TAG_END);
  198.  
  199.                                             ctext(win, "This text is not smoothed", 102);
  200.                                             ctext(win, "The difference is clearly", 128);
  201.                                             ctext(win, "visible.", 154);
  202.  
  203.                                             TT_SetAttrs(rp,
  204.                                               TT_Antialias, TT_Antialias_On,
  205.                                               TT_Transparency, 64,
  206.                                             TAG_END);
  207.  
  208.                                             ctext(win, "transparent text (75%)", 190);
  209.  
  210.                                             TT_SetAttrs(rp,
  211.                                               TT_Transparency, 128,
  212.                                             TAG_END);
  213.  
  214.                                             ctext(win, "transparent text (50%)", 216);
  215.  
  216.                                             TT_SetAttrs(rp,
  217.                                               TT_Transparency, 192,
  218.                                             TAG_END);
  219.  
  220.                                             ctext(win, "transparent text (25%)", 242);
  221.  
  222.                                             ReleasePen(win->WScreen->ViewPort.ColorMap, pen);
  223.                                           }
  224.                                       }
  225.                                   }
  226.                               }
  227.                           }
  228.                       }
  229.                     else PutStr("TT_SetFont() failed.\n");
  230.                     TT_CloseFont(font);
  231.                   }
  232.                 else PutStr("Font open failed.\n");
  233.  
  234.                 TT_DoneRastPort(rp);
  235.                 CloseWindow(win);
  236.               }
  237.             free_font_name(fontname);
  238.           }
  239.       }
  240.     cleanup();
  241.     return 0;
  242.   }
  243.